package com.sud.config.exception;

import com.sud.config.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 功能：Controller层全局异常拦截处理
 * 作者：SUD
 * 时间：2020/9/4 11:58
 */
@Api(tags = "全局异常捕获")
@RestController
@RestControllerAdvice
public class GlobalControllerExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    /**
     * 拦截所有未拦截异常
     * @param request
     * @param e
     * @return
     */
    @ApiOperation(value="异常捕获",produces="application/json",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="request",value="请求参数",required=true,dataType="HttpServletRequest"),
            @ApiImplicitParam(name="e",value="异常信息",required=true,dataType="Exception")
    })
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(HttpServletRequest request , Exception e){
        logger.error("系统出现异常，错误内容：{} ,出现错误的url：{}",e.getMessage(),request.getRequestURL());
        return Result.error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
    }
}
